﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Autorent.Desktop.Model.AutorentMngrServiceReference;

namespace Autorent.Desktop.Model.Services
{
    public class PaymentsRepository : AutorentEntitiesContext, IPaymentsRepository
    {
        public ObservableCollection<Payment> GetPayments()
        {
            try
            {
                return new ObservableCollection<Payment>(Context.Payments);
            }
            catch (Exception ex)
            {
                throw new AutorentModelException("Error occured while connecting service", ex.InnerException);
            }
        }

        public void AddPayment(Payment payment)
        {
            Context.AddToPayments(payment);
            try
            {
                Context.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new AutorentModelException("Error occured while connecting service", ex.InnerException);
            }
        }

        public void UpdatePayment(Payment payment)
        {
            Context.UpdateObject(payment);
            Context.SaveChanges();
        }

        public void DeletePayment(Payment payment)
        {
            try
            {
                Context.DeleteObject(Context.Payments.Where(p => p.Payment_ID.Equals(payment.Payment_ID)));
                Context.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new AutorentModelException("Error occured while deleting object", ex.InnerException);
            }
        }
    }
}
